<?php
namespace app\appclass\model;
use app\BaseModel;
use app\card\model\Job;
use app\shop\controller\IndexUser;
use app\shop\model\AdminCompany;
use app\shop\model\IndexUserInfo;
use think\facade\Db;



class CardList extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_appclass_card_list';


    protected $append = [

        'card_time',

        'class_card'

    ];


    /**
     * @author chenniang
     * @DataTime: 2021-05-13 18:09
     * @功能说明:支持的会员卡
     */
    public function getClassCardAttr($value,$data){

        if(!empty($data['id'])&&!empty($data['store_id'])){

            $class_card_model = new ClassCard();

//            $dis = [
//
//                'card_id' => $data['id'],
//            ];
//
//            $list = $class_card_model->where($dis)->select()->toArray();


            $dis = [

                'a.card_id' => $data['id'],

                'b.store_id' => $data['store_id'],

                'b.status'   => 1
            ];

            $list = $class_card_model->alias('a')
                    ->join('longbing_card_appclass_class_list b','a.class_id = b.id')
                    ->where($dis)
                    ->field('a.*')
                    ->select()
                    ->toArray();

            return $list;

        }


    }

    /**
     * @author chenniang
     * @DataTime: 2021-05-11 18:15
     * @功能说明:会员卡可用时间
     */
    public function getCardTimeAttr($value,$data){

        if(!empty($data['id'])){

            $card_time_model = new CardTime();

            $litst = $card_time_model->where(['card_id'=>$data['id']])->select()->toArray();

            return $litst;
        }

    }





    /**
     * @author chenniang
     * @DataTime: 2021-05-11 18:02
     * @功能说明:后台添加
     */
    public function adminDataAdd($data){

        $time = $data['card_time'];

        unset($data['card_time']);

        $class = $data['class_card'];

        unset($data['class_card']);

        $res = $this->dataAdd($data);

        $id = $this->getLastInsID();

        $this->updateSome($id,$time,$class,$data['uniacid']);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-11 18:05
     * @功能说明:编辑啊
     */
    public function updateSome($card_id,$time,$class,$uniacid){

        $card_time_model = new CardTime();

        $card_class_model = new ClassCard();

        $card_time_model->where(['card_id'=>$card_id])->delete();

        $card_class_model->where(['card_id'=>$card_id])->delete();

        if(!empty($time)){

            foreach ($time['week'] as $k=>$value){

                foreach ($time['time'] as $vs){

                    $insert = [

                        'uniacid' => $uniacid,

                        'card_id' => $card_id,

                        'week'    => $value,

                        'start_time' => $vs['start_time'],

                        'end_time' => $vs['end_time'],
                    ];

                    $card_time_model->insert($insert);
                }


            }

        }

        if(!empty($class)){

            foreach ($class as $k=> $v){

                $inset['uniacid'] = $uniacid;

                $inset['card_id'] = $card_id;

                $inset['class_id'] = $v['class_id'];

                $inset['value'] = !empty($v['value'])?$v['value']:0;

                $card_class_model->insert($inset);

            }


        }


        return true;
    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:00
     * @功能说明:添加
     */
    public function dataAdd($data){

        $data['create_time'] = time();

        $res = $this->insert($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-13 17:50
     * @功能说明:编辑
     */
    public function adminDataUpdate($dis,$data){

        $time = $data['card_time'];

        unset($data['card_time']);

        $class = $data['class_card'];

        unset($data['class_card']);

        $res = $this->dataUpdate($dis,$data);

        $this->updateSome($dis['id'],$time,$class,$data['uniacid']);

        return $res;


    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:03
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:04
     * @功能说明:详情
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:05
     * @功能说明:
     */
    public function dataList($dis,$page=10){

        $data = $this->where($dis)->where('status','>',-1)->order('top desc,id desc')->paginate($page)->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-12 09:39
     * @功能说明:教练
     */
    public function coach($store_id,$dis=[],$is_all=0){

        $user_model    = new IndexUserInfo();

        $company_model = new AdminCompany();

        $job_model  = new Job();
        //获取下级对部门
        $company = $company_model->companyLow($store_id);

        $company[] = $store_id;

        $where['is_staff'] = 1;

//        $where['is_class'] = 1;

        if($is_all==0){

            $where['class_status'] = 1;
        }

        $list = $user_model->where($where)->where('company_id','in',$company)->where(function ($query) use ($dis){
            $query->whereOr($dis);
        })->field('fans_id,uniacid,company_id,job_id,phone,is_class,class_status')->select()->toArray();

        if(!empty($list)){

            foreach ($list as &$v){

                $v['name']     = lbUserName($v['fans_id']);

                $v['avatar']   = lbUserAvatar($v['fans_id']);

                $v['job_name'] = $job_model->where(['id'=>$v['job_id']])->value('name');


            }

        }

        return $list;

    }

    /**
     * @author chenniang
     * @DataTime: 2021-06-16 15:29
     * @功能说明:获取私交课教练
     */
    public function privateClassCoach($uniacid,$cate_id=0){

        $class_model = new ClassList();

        $dis = [

            'a.uniacid' => $uniacid,

            'b.type'    => 1,

            'a.status'  =>1,

        ];

        if(!empty($cate_id)){

            $dis['b.id'] = $cate_id;
        }

        $data = $class_model->alias('a')
            ->join('longbing_card_appclass_class_cate b','a.cate_id = b.id')
            ->join('longbing_card_appclass_class_coach c','a.id = c.class_id')
            ->where($dis)
            ->group('c.coach_id')
            ->column('c.coach_id');

        return $data;
    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-16 15:29
     * @功能说明:获取含有课教练
     */
    public function classCoach($uniacid){

        $class_model = new ClassList();

        $dis = [

            'a.uniacid' => $uniacid,

            'a.status'  =>1,

        ];

        $data = $class_model->alias('a')
            ->join('longbing_card_appclass_class_coach c','a.id = c.class_id')
            ->where($dis)
            ->group('c.coach_id')
            ->column('c.coach_id');

        return $data;
    }

    /**
     * @author chenniang
     * @DataTime: 2021-05-12 09:39
     * @功能说明:教练
     */
    public function coachList($store_id,$dis=[],$page=10){

        $user_model    = new IndexUserInfo();

        $user = new \app\shop\model\IndexUser();

        $company_model = new AdminCompany();

        $job_model  = new Job();
        //获取下级对部门
        $company = $company_model->companyLow($store_id);

        $company[] = $store_id;

        $list = $user_model->where($dis)->where('is_staff','=',1)->where('company_id','in',$company)->field('fans_id,uniacid,company_id,job_id,phone,is_class,class_status')->paginate($page)->toArray();

        if(!empty($list['data'])){

            foreach ($list['data'] as &$v){

                $v['department_name'] = $company_model->where(['id'=>$v['company_id']])->where('pid','<>',0)->value('name');

                $v['nickName'] = $user->where(['id'=>$v['fans_id']])->value('nickName');

                $v['job_name'] = $job_model->where(['id'=>$v['job_id']])->value('name');

                $v['name']     = lbUserName($v['fans_id']);

                $v['avatarUrl'] =lbUserAvatar($v['fans_id']);


            }
        }

        return $list;

    }




    /**
     * @author chenniang
     * @DataTime: 2021-06-02 10:01
     * @功能说明:主要检查名字不能重复
     */
    public function check($data){

        $dis[] = ['uniacid','=',$data['uniacid']];

        $dis[] = ['title','=',$data['title']];

        if(!empty($data['id'])){

            $dis[] = ['id','<>',$data['id']];
        }

        $info = $this->dataInfo($dis);

        if(!empty($info)){

            $this->errorMsg('已有该名字的会员卡');
        }

        return true;

    }



}